<?php 
// Modified 9/19/07 by Mark Pearson to simplify.
// Modified 7/16/08 by Charles Fulton to add module counter and generalized database prefix

require '../config.php';
require_once('../lib/datalib.php');

// global vars which are poplated by loadRoles ;
$faculty_role = null ; 
$student_role = null ; 

loadRoles(); 

//variables
$courseid=156;
$teachers     = array();
$teach_active = array();
$student_active = array();
$class_size_distro_small = array();
$class_size_distro_large = array();
$active_courses = 0;
$module_creation_time_buffer = 15 * 60;	// wait fifteen minutes before counting module in _course_modules
$database_prefix = 'mdl';		// database prefix; default is mdl

// code to make a pucker xhtml page
$initial_code = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Statistics of Moodle Use</title>
  </head>
  <body>
 <h1>Statistics for Moodle Site :</h1>';

$final_code= '<hr>
<p>Dylan Leeman and Mark Pearson (Earlham) working with code written by Matt Bockol (Carleton)</p>
<p>16-Oct-2007<p>
</body>
</html>';

//Formatting variables
//$seperatortext=':';
//$linebreaktext='<br>';
$seperatortext='</td><td>';
$linebreaktext='</td></tr>';
/*
echo '<table border=0 cellpadding=5 cellspacing=0 rules="all"><tr><td>';
  

//Print Column Headers
echo 'shortname';
echo $seperatortext;
echo 'fullname';
echo $seperatortext;
echo 'number of teachers';
echo $seperatortext;
echo 'number of students';
echo $seperatortext;
echo 'forum posts';
echo $seperatortext;
echo 'teacher posts';
echo $seperatortext;
// echo 'teacher posts (responses)';
// echo $seperatortext;
echo 'student posts';
echo $seperatortext;
// echo 'posts per student';
// echo $seperatortext;
echo 'quiz';
echo $seperatortext;
echo 'resources(total)';
echo $seperatortext;
echo 'resources(links)';
echo $seperatortext;
echo 'resources(moodle text)';
echo $seperatortext;
echo 'resources(moodle html)';
echo $seperatortext;
echo 'resources(files-notlinks)';
echo $seperatortext;
echo 'resources(pdf)';
echo $seperatortext;
echo 'resources(pdf-local)';
echo $seperatortext;
echo 'resources(.doc)';
echo $seperatortext;
echo 'resources(.xls)';
echo $seperatortext;
echo 'resources(.ppt)';
echo $seperatortext;
echo 'resources local htm';
echo $seperatortext;
echo 'resources (images)';
echo $seperatortext;
echo 'resources (audio)';
echo $seperatortext;
echo 'resources (video)';
echo $seperatortext;
echo 'assignments';
echo $seperatortext;
echo 'assignments (offline)';
echo $seperatortext;
echo 'assignments (uploadsingle)';
echo $seperatortext;
echo 'assignments (online)';
echo $seperatortext;
echo 'assignments (uploadreview)';
echo $seperatortext;
// echo 'chats';
// echo $seperatortext;
// echo 'chat messages';
// echo $seperatortext;
echo 'glossaries';
echo $seperatortext;
// echo 'glossary entries';
// echo $seperatortext;
echo 'wikis';
echo $seperatortext;
echo 'wiki pages';
echo $seperatortext;
echo 'wiki contributors';
echo $seperatortext;
// echo 'journals';
// echo $seperatortext;
// echo 'journal entries';
// echo $seperatortext;
// echo 'choice';
// echo $seperatortext;
// echo 'lesson';
// echo $seperatortext;
echo 'groups';
echo $seperatortext;
// echo 'scales';
// echo $seperatortext;
// echo 'scorm';
// echo $seperatortext;
// echo 'surveys';
// echo $seperatortext;
// echo 'workshops';
// echo $seperatortext;
// echo 'workshops (submissions)';
// echo $seperatortext;
echo 'Teachers';
//completion percentage
//teacher replies to student posts?
//track evaluation

echo $linebreaktext;
*/
//Get course numbers as array
$courseidarray=get_fieldset_sql("Select distinct id from $database_prefix"."_course");

foreach ($courseidarray as $courseid)
{
	
	//echo '<tr><td>';
    
    //Get course shortname
    //echo get_field('course', 'shortname','id', $courseid) ;
    //echo $seperatortext;

    //Get course Fullname
    //echo get_field('course', 'fullname','id', $courseid) ;
    //echo $seperatortext;

    // collect the course faculty and students
    $course_teachers = getCourseMembersByID($courseid, $faculty_role) ; 
    $course_students = getCourseMembersByID($courseid, $student_role) ;

    $course_teachers_ids = array();
    $course_students_ids = array();

    foreach($course_teachers as $teacher){ array_push($course_teachers_ids, $teacher->id); }
    foreach($course_students as $student)
    { 
	    array_push($course_students_ids, $student->id); 
    }
    
    //Count the number of teachers
    //echo count($course_teachers);
    //echo $seperatortext;

    //Get enrollment -- This may differ on each moodle install
    $classsize = count($course_students); 
    //echo $classsize ; 
    //echo $seperatortext ; 
    
    //Count forum posts
    //Do we need to add discussionas and forums to the count? -> check a few examples && the db
    $num_forum_posts = count_records_sql("SELECT count($database_prefix"."_forum_posts.id) 
			from $database_prefix"."_forum_posts,$database_prefix"."_forum_discussions 
			where $database_prefix"."_forum_discussions.id = $database_prefix"."_forum_posts.discussion 
			and $database_prefix"."_forum_discussions.course ='$courseid'");
    //echo $seperatortext;
    
    //Count teacher posts
    // Do we need to add discussions and forums to the count? --- just forums
    $teacher_posts = count_records_sql("SELECT count($database_prefix"."_forum_posts.id) 
   			from $database_prefix"."_forum_posts, $database_prefix"."_forum_discussions where
   			$database_prefix"."_forum_posts.userid in ('" . implode(',',$course_teachers_ids) . "') 
   			and $database_prefix"."_forum_discussions.id = $database_prefix"."_forum_posts.discussion 
   			and $database_prefix"."_forum_discussions.course = '$courseid'");  

    //echo $seperatortext;
    
     //Count teacher posts (responses
    // Do we need to add discussions and forums to the count? --- just forums
    /*echo count_records_sql("SELECT count($database_prefix"."_forum_posts.id) from $database_prefix"."_forum_posts, 
			$database_prefix"."_forum_discussions where $database_prefix"."_forum_posts.parent !=0 and 
			$database_prefix"."_forum_posts.userid in ('" . implode(',',$course_teachers_ids) . "') and 
			$database_prefix"."_forum_discussions.id = $database_prefix"."_forum_posts.discussion and 
			$database_prefix"."_forum_discussions.course = '$courseid'");*/

    //echo $seperatortext;
    
     //Count student posts
    // Do we need to add discussions and forums to the count?--no
    $studentposts = count_records_sql("SELECT count($database_prefix"."_forum_posts.id) 
    			from $database_prefix"."_forum_posts, $database_prefix"."_forum_discussions where
    			$database_prefix"."_forum_posts.userid in ('" . implode(',', $course_students_ids) . "') 
    			and $database_prefix"."_forum_discussions.id = $database_prefix"."_forum_posts.discussion 
    			and $database_prefix"."_forum_discussions.course = '$courseid'"); 

    //echo $studentposts;
    //echo $seperatortext;
    
    //calculate student posts
    //if($classsize!=0){echo $studentposts/$classsize;}
    //else echo 'NULL';
    //echo $seperatortext;
    
    //Count quizes
    //echo count_records('quiz','course', $courseid);
    //echo $seperatortext;
    
    //How do we measure quiz #/quality?
    
    //count total modules (use time limit)
    $num_modules = count_records_sql("SELECT count($database_prefix"."_course_modules.module) 
    			from $database_prefix"."_course_modules inner join $database_prefix"."_course on 
    			$database_prefix"."_course_modules.course=$database_prefix"."_course.id WHERE 
    			$database_prefix"."_course_modules.added > ($database_prefix"."_course.timecreated + $module_creation_time_buffer)");
    // echo $num_modules;
    
    //count total resources
    $num_resources = count_records('resource','course', $courseid);
    
    //echo "<p>$courseid:  " . $num_resources . "</p>\n";
    //echo $seperatortext;
    
    //Count resources(links)
    //not like hrc2 discounts HRC streaming files
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='file' 
    //		and reference like 'http%' and reference not like '%hrc2%' and course='$courseid'");
    //echo $seperatortext;
    
    //Count moodle text
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='text' and course='$courseid'");
    //echo $seperatortext;
    
    //Count moodle html
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='html' and course='$courseid'");    
    //echo $seperatortext;
    
    //Count files-notlinks
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='file' and reference not like 'http%' and course='$courseid'");
    //echo $seperatortext;
    
    //Count pdfs
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where reference like '%pdf' and course='$courseid'");
    //echo $seperatortext;
    
    //Count pdfs-local
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where reference like '%pdf' and reference not like 'http%' and course='$courseid'");
    //echo $seperatortext;
   
    //Count .doc
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where reference like '%doc' and course='$courseid'");
    //echo $seperatortext;
    
    //Count .xls
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where reference like '%xls' and course='$courseid'");
    //echo $seperatortext;
    
    //Count .ppt
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where reference like '%pp' and course='$courseid'");
    //echo $seperatortext;
    
    //Count local htm, not moodle html
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='file' and reference like '%htm' and reference not like 'http%' and course='$courseid'");
    //echo $seperatortext;
    
    //Count images
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='file' 
    //		and (reference like '%jpg' or reference like '%png' or reference like '%tif' 
    //		or reference like '%gif') and course='$courseid'");
    //echo $seperatortext;
    
    //Count audio
    //echo count_records_sql("SELECT count(id) FROM $database"."_resource where type='file' 
    //		and (reference like '%.mp3' or reference like '%.wav') and course='$courseid'");
    //echo $seperatortext;
    
    //Count video
    //echo count_records_sql("SELECT count(id) FROM $database_prefix"."_resource where type='file' 
    //		and (reference like '%.mov' or reference like '%.mp4' or reference like '%.rm' 
    //		or reference like '%.avi') and course='$courseid'");
    //echo $seperatortext;
    
    //To do - add dots '.' to extensions
    
    //Count assignments
     //echo count_records('assignment','course', $courseid);
     //echo $seperatortext;
     
     //Count assignments (offline)
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_assignment where assignmenttype='offline' and course='$courseid'");
     //echo $seperatortext;
     
     //Count assignments (uploadsingle)
     //echo count_records_sql("SELECT count(id) from $databaes_prefix"."_assignment where assignmenttype='uploadsingle' and course='$courseid'");
     //echo $seperatortext;
     
    //Count assignments (online)
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_assignment where assignmenttype='online' and course='$courseid'");
     //echo $seperatortext;
     
      //Count assignments (uploadreview)
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_assignment where assignmenttype='uploadreview' and course='$courseid'");
     //echo $seperatortext;
     
      //Count chats
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_chat where course='$courseid'");
     //echo $seperatortext;
     
     //Count chat messages
     //echo count_records_sql("SELECT count($database_prefix"."_chat_messages.id) from $database_prefix"."_chat_messages,$database_prefix"."_chat 
     //		where $database_prefix"."_chat_messages.chatid=$database_prefix"."_chat.id and $database_prefix"."_chat.course='$courseid'");
     //echo $seperatortext;
     
      //Count glossaries
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_glossary where course='$courseid'");
     //echo $seperatortext;
     
     //Count glossary entries
     //echo count_records_sql("SELECT count($database_prefix"."_glossary_entries.id) from 
     //		$database_prefix"."_glossary_entries,$database_prefix"."_glossary where
     //		$database_prefix"."_glossary_entries.glossaryid=$database_prefix"."_glossary.id 
     //		and $database_prefix"."_glossary.course='$courseid'");
     //echo $seperatortext;
     
      //Count wikis
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_wiki where course='$courseid'");
     //echo $seperatortext;
     
     //Count wiki pages (more accurately, edits)
     //echo count_records_sql("SELECT count($database_prefix"."_wiki_pages.id) from $database_prefix"."_wiki_pages,$database_prefix"."_wiki 
     //		where $database_prefix"."_wiki_pages.wiki=$database_prefix"."_wiki.id 
     //		and $database_prefix"."_wiki.course='$courseid'");
     //echo $seperatortext;
     
     //Count wiki contributors
     //echo count_records_sql("SELECT count(distinct $database_prefix"."_wiki_pages.userid) from $database_prefix"."_wiki_pages,$database_prefix"."_wiki
     //		where $database_prefix"."_wiki_pages.wiki=$database_prefix"."_wiki.id 
     //		and $database_prefix"."_wiki.course='$courseid'");
     //echo $seperatortext;
     
     //Count journals
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_journal where course='$courseid'");
     //echo $seperatortext;
     
     //Count journal entries
     //echo count_records_sql("SELECT count($database_prefix"."_journal_entries.id) from 
     //		$database_prefix"."_journal_entries,$database_prefix"."_journal where 
     //		$database_prefix"."_journal_entries.journal=$database_prefix"."_journal.id 
     //		and $database_prefix"."_journal.course='$courseid'");
     //echo $seperatortext;
     
     //Count choice
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_choice where course='$courseid'");
     //echo $seperatortext;
     
     //Count lesson
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_lesson where course='$courseid'");
     //echo $seperatortext;
     
     //Count groups
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_groups where course='$courseid'");
     //echo $seperatortext;
    
     //Count scales
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_scale where course='$courseid'");
     //echo $seperatortext;
    
    //Count scorm
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_scorm where course='$courseid'");
     //echo $seperatortext;
     
     //Count surveys
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_survey where course='$courseid'");
     //echo $seperatortext;
     
     //Count workshops
     //echo count_records_sql("SELECT count(id) from $database_prefix"."_workshop where course='$courseid'");
     //echo $seperatortext;
     
     //Count workshop submissions
     //echo count_records_sql("SELECT count($database_prefix"."_workshop_submissions.id) from 
     //		$database_prefix"."_workshop_submissions,$database_prefix"."_workshop where 
     //		$database_prefix"."_workshop_submissions.workshopid=$database_prefix"."_workshop.id 
     //		and $database_prefix"."_workshop.course='$courseid'");
     //echo $seperatortext;
     
    //Get teachers
    $teach1 = get_fieldset_sql("Select Concat($database_prefix"."_user.lastname,$database_prefix"."_user.firstname) 
    		from $database_prefix"."_user where $database_prefix"."_user.id in ('" . implode(',', $course_teachers_ids) . "')");
    
    //if(!$teach1) echo "NULL";

    $cat_id   = get_field( 'course', 'category', 'id', $courseid );
    $cat_name = get_field( 'course_categories', 'name', 'id', $cat_id );

    foreach($teach1 as $teachname)
    	{
	    if ( $num_forum_posts >= 1 || $num_resources >= 1 || $num_modules >= 1)
	    {
		    if ( $teachname == 'Tobey Announsd' && count( $teach1 ) == 1 )
		    {
			    echo "<p>Course:  " . $courseid . " is active and has 'Tobey Announsd' as its teacher.</p>\n";
		    }
		    else
		    {
			    $teach_active[$teachname] += 1;
		    }
	    } 
    }
    if ( $num_forum_posts >= 1 || $num_resources >= 1 || $num_modules >= 1)
    {
	    
	    $dept_active[$cat_name] += 1;
	    foreach( $course_students_ids as $student )
	    {
		    $student_active[$student] += 1;
	    }
	    if ( $classsize < 101 && $classsize > 0)
	    {
		    $class_size_distro_small[ceil($classsize/10)] += 1;
	    }
	    else if ( $classsize > 0 )
	    {
		    $class_size_distro_large[ceil($classsize/10)] += 1;
	    }
	    $active_courses++;
    }
    //echo $seperatortext;
     
    //echo $linebreaktext;
    
}//end for() loop

// start building page
echo $initial_code;
echo '<table>
	<tr><td>Teachers w/ Active Courses:</td><td align="right">' . count( $teach_active ) . '</td></tr>';
echo '<tr><td>Number Active Courses:</td><td align="right">' .  $active_courses . '</td></tr>
</table>';

// echo "<p>Teachers w/ Active Courses: " . count( $teach_active ) . "</p>\n";
// echo "<p>Active Courses: " .  $active_courses . "</p>\n";

echo '<h3>Distribution of Courses amongst Teachers</h3> 
<p>How many teachers teach a single course in Moodle, how many with two, etc?</p>
<table rules="all">
	<tr><th># courses taught</th><th>teacher numbers</th></tr>';
$distribution = distribution( $teach_active );
foreach ( array_keys( $distribution ) as $seg_idx )
{
	echo '	<tr><td align="right">' . $seg_idx . '</td><td align="right">' . $distribution[$seg_idx] . '</td></tr>';
}
echo '</table>';
//
echo ' <h3>Students taking courses</h3>
<p>How many students have a single course in Moodle, how many with two, three etc?</p>
 <table rules="all">
	<tr><th># courses taken</th><th># students </th></tr>';

$stu_distro = distribution( $student_active );
foreach ( array_keys( $stu_distro ) as $seg_idx )
{
    echo '	<tr><td align="right">' . $seg_idx . '</td><td align="right">' . $stu_distro[$seg_idx] . '</td></tr>';
}
echo '</table>';

echo '<h3>Distribution of class sizes with number of courses</h3>
<p>How many course of class size 1-9? 10-19 ? etc</p>
<p>Class Size Distro:</p>
 <table rules="all">
	<tr><th>Class size</th><th># courses</th></tr>';
foreach ( array_keys( $class_size_distro_small ) as $seg_idx )
{
	echo '	<tr><td align="right">';
       echo ($seg_idx-1)*10 + 1 . "-" . ($seg_idx)*10 . '</td><td align="right">' . $class_size_distro_small[$seg_idx] . '</td></tr>'; 
}
foreach ( array_keys( $class_size_distro_large ) as $seg_idx )
{
	echo '	<tr><td align="right">';
	echo ($seg_idx-1)*20 + 1 . "-" . ($seg_idx)*20 . '</td><td align="right">' . $class_size_distro_small[$seg_idx] . '</td></tr>';
}
echo '</table>';

echo '<h3>Active Courses By Department:</h3>
 <table rules="all">
	<tr><th>Department name</th><th># active courses</th></tr>';

foreach ( array_keys( $dept_active ) as $which_dept )
{
	echo '<tr><td align="center">' . $which_dept . '</td><td align="right">' . $dept_active[$which_dept] . '</td></tr>';
}
echo '</table><p>&nbsp;</p>';

echo $final_code;

exit;

function loadRoles()
{

    global $student_role ; 
    global $faculty_role ;

    // only look up the role id if we haven't already
    if( isset($student_role) && isset($faculty_role) ){ return TRUE ; }

    // Get all the possible roles
    $roles = get_records('role');
    $role_count = 0 ; 

    foreach($roles as $this_role){

        if(strcmp($this_role->name, 'Student') == 0){
            $student_role = $this_role ;
            $role_count++ ; 
            }

        if(strcmp($this_role->name, 'Teacher') == 0){
            $faculty_role = $this_role ;
            $role_count++ ; 
            }

        }

    // make sure we collected student and faculty roles
    if($role_count == 2){ return TRUE ; }

    echotime("Unable to determine student and faculty roles.\n");
    die ;
}


function getCourseMembersByID($course_id = null, $role)
{
    // if we're not passed a course id we can't proceed
    if($course_id == null){ return Array() ; }

    // if we're not passed a role we can't proceed
    if($role == null){ return Array(); }

    // get the course context
    $context = get_context_instance(CONTEXT_COURSE, $course_id);

    // get the list of users who currently have this role
    $moodle_enrolled_people = get_role_users($role->id , $context);

    return $moodle_enrolled_people ;
}

function distribution( &$active_teachers_count )
{
	$distribution = array();
	foreach ( $active_teachers_count as $teacher )
	{
		$distribution[$teacher] += 1;
	}
	return $distribution;
}	







?>
